package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.BillRecord;
import org.ahuanlien.crm.domain.CheckOrder;
import org.ahuanlien.crm.domain.Product;
import org.ahuanlien.crm.domain.TemOrder;
import org.ahuanlien.crm.mapper.*;
import org.ahuanlien.crm.query.TemOrderQueryObject;
import org.ahuanlien.crm.service.ITemOrderService;
import org.ahuanlien.crm.util.PageResult;
import org.ahuanlien.crm.util.SnGeneratorUtil;
import org.ahuanlien.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TemOrderServiceImpl implements ITemOrderService {
	@Autowired
	TemOrderMapper temOrderMapper;

	@Autowired
	BillRecordMapper billRecordMapper;

	@Autowired
	ProductMapper productMapper;

	@Autowired
	ClientMapper clientMapper;

	@Autowired
	CarMapper carMapper;


	@Autowired
	CheckOrderMapper checkOrderMapper;


	public int deleteByPrimaryKey(Long id) {
		TemOrder temOrder = temOrderMapper.selectByPrimaryKey(id);
		BillRecord billRecord = new BillRecord();
		billRecord.setSn(temOrder.getSn());
		billRecord.setHolder(temOrder.getHolder());
		billRecord.setCar(temOrder.getCarNo());
		billRecord.setADate(temOrder.getADate());
		billRecord.setYear(temOrder.getYear());
		billRecord.setOpUser(temOrder.getOpUser());
		billRecord.setTotalAmount(temOrder.getTotalAmount());
		billRecord.setState("拒保");
		billRecord.setItem("保险产品");
		billRecordMapper.insert(billRecord);
		return temOrderMapper.deleteByPrimaryKey(id);
	}

	public int insert(TemOrder record) {
		//当前保险商品信息
		Product product = productMapper.selectByPrimaryKey(record.getProduct().getId());
		String pn = SnGeneratorUtil.getSn("pn");//保单编号
		record.setSn(pn);//设置
		record.setADate(new Date());//创建日期
		record.setOpUser(UserContext.getCurrentUser());//业务员
		record.setState(true);
		record.setTotalAmount(product.getAnnualFee().multiply(new BigDecimal(record.getYear())));

		//保存暂存表明细
		BillRecord billRecord = new BillRecord();
		billRecord.setSn(record.getSn());
		billRecord.setADate(record.getADate());
		billRecord.setHolder(record.getHolder());
		billRecord.setCar(record.getCarNo());
		billRecord.setPDate(record.getPDate());
		billRecord.setYear(record.getYear());
		billRecord.setOpUser(record.getOpUser());
		billRecord.setTotalAmount(record.getTotalAmount());
		billRecord.setState("暂存");
		billRecord.setItem("查看产品明细");
		billRecordMapper.insert(billRecord);
		return temOrderMapper.insert(record);

	}

	public TemOrder selectByPrimaryKey(Long id) {
		return temOrderMapper.selectByPrimaryKey(id);
	}

	public List<TemOrder> selectAll() {
		return temOrderMapper.selectAll();
	}

	public int updateByPrimaryKey(TemOrder record) {
		//当前保险商品信息
		Product product = productMapper.selectByPrimaryKey(record.getProduct().getId());
		String pn = SnGeneratorUtil.getSn("pn");//保单编号
		record.setSn(pn);//设置
		record.setADate(new Date());//创建日期
		record.setOpUser(UserContext.getCurrentUser());//业务员
		record.setState(true);
		record.setTotalAmount(product.getAnnualFee().multiply(new BigDecimal(record.getYear())));

		//当前客户
		clientMapper.updateByTemOrder(record.getHolder());
		//当前车辆
		carMapper.updateByTemOrder(record.getCarNo());
		//当前产品
		productMapper.updateByTemOrder(record.getProduct());

		return temOrderMapper.updateByPrimaryKey(record);
	}

	public PageResult query(TemOrderQueryObject qo) {
		int count = temOrderMapper.queryForCount();
		List<TemOrder> list = temOrderMapper.queryForList(qo);
		if (count == 0) {
			return new PageResult(0, Collections.EMPTY_LIST);
		}
		return new PageResult(count, list);
	}

	@Override
	public void audit(Long id) {
		TemOrder temOrder = temOrderMapper.selectByPrimaryKey(id);
		//把信息存入审核单
		CheckOrder checkOrder = new CheckOrder();
		checkOrder.setSn(temOrder.getSn());
		checkOrder.setHolder(temOrder.getHolder());
		checkOrder.setCarNo(temOrder.getCarNo());
		checkOrder.setADate(temOrder.getADate());
		checkOrder.setYear(temOrder.getYear());
		checkOrder.setOpUser(temOrder.getOpUser());
		checkOrder.setTotalAmount(temOrder.getTotalAmount());
		checkOrder.setState(temOrder.getState());
		checkOrder.setItem("保险产品明细");
		checkOrderMapper.insert(checkOrder);
		temOrderMapper.deleteByPrimaryKey(id);

		//把信息存入单据历史记录
		BillRecord billRecord = new BillRecord();
		billRecord.setSn(temOrder.getSn());
		billRecord.setHolder(temOrder.getHolder());
		billRecord.setCar(temOrder.getCarNo());
		billRecord.setADate(temOrder.getADate());
		billRecord.setYear(temOrder.getYear());
		billRecord.setOpUser(temOrder.getOpUser());
		billRecord.setTotalAmount(temOrder.getTotalAmount());
		billRecord.setState("已提交待审核");
		billRecord.setItem("保险产品明细");
		billRecordMapper.insert(billRecord);
	}
}
